package com.springboot.helloworld.controller;

import com.springboot.helloworld.bean.Result;
import com.springboot.helloworld.entity.Girl;
import com.springboot.helloworld.repository.GirlRepository;
import com.springboot.helloworld.service.GirlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @version v1.0
 * @description: 女生控制器
 * @author: xiaoliang.liu
 * @date: 2018/7/1 20:59
 */
@RestController
@Slf4j
public class GirlController extends BaseController {

    private static final int ARGS_CHECK_FAIL = 1;

    /**
     * 女生 service
     */
    private final GirlService girlService;

    /**
     * 女生dao接口
     */
    private final GirlRepository girlRepository;

    @Autowired
    public GirlController(GirlRepository girlRepository, GirlService girlService) {
        this.girlRepository = girlRepository;
        this.girlService = girlService;
    }

    /**
     * 获得所有女生
     *
     * @return list
     */
    @GetMapping("/girls")
    public List<Girl> listGirl() {
        return girlRepository.findAll();
    }

    /**
     * 根据id查询一个女生
     *
     * @param id 女生id
     * @return Girl
     */
    @GetMapping("girl/{id}")
    public Optional<Girl> getGirl(@PathVariable("id") Integer id) {
        return girlRepository.findById(id);
    }

    /**
     * 查找年龄相同的女生列表
     *
     * @param age 女生年龄
     * @return list
     */
    @GetMapping("girl/age/{age}")
    public List<Girl> listGirlByAge(@PathVariable("age") Integer age) {
        return girlRepository.findByAge(age);
    }

    /**
     * 添加一个女生
     * girl 女生参数对象
     *
     * @return 添加结果
     */
    @PostMapping("/girl")
    public Result<Object> saveGirls(@Valid Girl girl, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("参数校验失败: {}", bindingResult.getFieldError().getDefaultMessage());
            return fail(ARGS_CHECK_FAIL, bindingResult.getFieldError().getDefaultMessage());
        }
        Girl saveResult = girlRepository.save(girl);
        return success(saveResult);
    }

    /**
     * 更新一个女生的资料
     * girl 女生参数对象
     *
     * @param id 女生id
     */
    @PutMapping("/girl/{id}")
    public Map<String, Object> updateGirl(@PathVariable("id") Integer id, Girl girl) {
        girl.setId(id);

        Girl saveResult = girlRepository.save(girl);

        HashMap<String, Object> map = new HashMap<>(3);
        map.put("code", "0000");
        map.put("desc", "更新成功");
        map.put("result", saveResult);

        return map;
    }

    /**
     * 根据id删除一个女生
     *
     * @param id 女生id
     */
    @DeleteMapping("/girl/{id}")
    public void removeGirl(@PathVariable("id") Integer id) {
        girlRepository.deleteById(id);
    }


    @GetMapping("/girl/getAge/{id}")
    public Integer getAge(@PathVariable("id") Integer id) throws Exception {
        return girlService.getAge(id);
    }

}
